﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace Silverlight4
{
    public partial class MainPage : UserControl
    {
        public class Writer
        {
            public Writer()
            {
            }
            public Writer(string writername, ObservableCollection<string> listOfTypes, string writerTitle)
            {
                Name = writername;
                Types = listOfTypes;
                Title = writerTitle;
            }
            public string Name { get; set; }
            public ObservableCollection<string> Types { get; set; }
            public string Title { get; set; }
        }

        public MainPage()
        {
            InitializeComponent();
            ObservableCollection<Writer> writers = new ObservableCollection<Writer>
            {
                new Writer ("Bush",new ObservableCollection<string>() {"ComboBox", "ComboBoxItem"},"writer one"),
                new Writer ("Clinton", new ObservableCollection<string>() {"ListBox","ListBoxItem"}, "writer two"),
                new Writer ("Obama", new ObservableCollection<string>() {"DataGrid","Binding"}, "writer three")
            };
            this.listbox1.DataContext = writers;
            this.dataGrid1.DataContext = writers;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.button.Content = "Click again";
            this.storyboard.Begin();
        }

        private void RootPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
            switch (originalSender.Name)
            {
                case "block1":
                    block1.Text += "Clicked";
                    break;
                case "block2":
                    block2.Text += "Clicked";
                    break;
                case "block3":
                    block3.Text += "Clicked";
                    break;
                default:
                    break;
            }

        }

        ToolTip tooltip1 = new ToolTip();
        private void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            
            if (tooltip1.IsOpen == false)
            {
                tooltip1.PlacementTarget = textbox;
                tooltip1.Content = "hello ToolTip";
                tooltip1.IsOpen = true;
            }
        }

        private void textbox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.tooltip1.IsOpen = false;
        }

        private void btnBold_Click(object sender, RoutedEventArgs e)
        {
            if (richTextbox1.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && (FontWeight)richTextbox1.Selection.GetPropertyValue(Run.FontWeightProperty) == FontWeights.Normal)
                richTextbox1.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
            else
                richTextbox1.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);

        }

        private void btnItalic_Click(object sender, RoutedEventArgs e)
        {
            if (richTextbox1.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle && (FontStyle)richTextbox1.Selection.GetPropertyValue(Run.FontStyleProperty) == FontStyles.Normal)
                richTextbox1.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
            else
                richTextbox1.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
        }

        private void btnUnderline_Click(object sender, RoutedEventArgs e)
        {
            if (richTextbox1.Selection.GetPropertyValue(Run.TextDecorationsProperty) == null)
                richTextbox1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
            else
                richTextbox1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
        }

        private void btnHyperLick_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink link = new Hyperlink();
            link.Inlines.Add("http://www.google.com");
            link.NavigateUri= new Uri("http://www.google.com");

            Paragraph p = new Paragraph();
            p.Inlines.Add(link);

            richTextbox1.Blocks.Add(p);
        }

        private void btnImage_Click(object sender, RoutedEventArgs e)
        {
            Image img = new Image();
            img.Source = new System.Windows.Media.Imaging.BitmapImage(
                new Uri("msdn.png",UriKind.RelativeOrAbsolute));
            img.Width = img.Height = 50;

            InlineUIContainer con = new InlineUIContainer();
            con.Child = img;

            Paragraph p = new Paragraph();
            p.Inlines.Add(con);

            richTextbox1.Blocks.Add(p);
        }

        
        private void btnIsReadOnly_Click(object sender, RoutedEventArgs e)
        {
            if (this.richTextbox1.IsReadOnly == true)
                this.richTextbox1.IsReadOnly = false;
            else
                this.richTextbox1.IsReadOnly = true;
        }
    }
}
